from indexer import *
from searchSort import *
import file_operations as file_op
import os
class model:
    
    def __init__(self):
        self.myIndexer= indexer()
        self.mySearcher = searchSort()
    
    def remove(self, file):
        self.myIndexer.removefromIndex(file)
        self.dialog(file + ' has been removed from the index.')
    
    def saveIndex(self,location):
        self.myIndexer.storeIndex(location)
        self.dialog('Index saved as ' + indexloc)
    
    def saveResults(self, location):
        file_op.write(location, self.myView.resultfield.text) 
        self.dialog('Search Results Saved')
    
    def addFolder(self,location):
        for paths in os.walk(location):
            for file in paths[2]:
                if not paths[0].endswith('\\'):
                    paths[0] += '\\'
                self.myIndexer.addtoIndex(paths[0]+file)
        self.dialog("All files in folder indexed.")                  
    
    def addFile(self,location):
        boolean = self.myIndexer.addtoIndex(location)
        if boolean:
            name = location.rpartition('\\')
            self.dialog(name[len(name) - 1] + ' successfully indexed.')
        else:
            self.dialog('Error: File to be Indexed Not Found.')   
         
    def clearIndex(self):
        self.myIndexer.doclist = []    
        self.dialog('Index has been cleared.')        
    
    def query(self, querylist, paramlist, dispDic, sort, reversed):
        if (len(querylist) == 0):
            self.dialog('Please Enter Valid Search Terms')
        else:
            results = []
            self.myView.resultfield.text = ''
            for query in querylist:
                self.output(query)
                self.setParameters(paramlist)
                self.mySearcher.printOptions = dispDic
                self.mySearcher.reversed = reversed
                self.mySearcher.sortMethod = self.mySearcher.sortMethods[sort]
                results = results + self.mySearcher.search(self.myIndexer.doclist,query)
            self.displayResults(results)
    
    def displayResults(self, results):
        for string in results:
            self.myView.display(string)
    
    def setParameters(self, paramlist):
        self.mySearcher.numResults = paramlist[0]
        self.mySearcher.wordcountweight = paramlist[1]
        self.mySearcher.consecutiveweight = paramlist[2]
    
    def openIndex(self,location):
        (file_name, file_type) = self.myIndexer.identifyType(location)
        if(file_type == 'index'):
            self.myIndexer.openIndex(location)
            self.dialog('Index at ' + findexloc + ' loaded')
        else:
            self.dialog('Not a Valid Index.')
        
    def getDocs(self):
        return self.myIndexer.getDocNames()
    
    def output(self,text):
        self.myView.display(text)
    
    def dialog(self, text):
        self.myView.dialog(text)